#include <vector>
using namespace std;
class Solution
{
public:
    int trap(vector<int> &height)
    {
        int leftMax = 0;
        int rightMax = 0;
        int leftIndex = 0;
        int rightIndex = height.size() - 1;
        int ans = 0;
        while (leftIndex <= rightIndex)
        {
            if (leftMax < rightMax)
            {
                if (height[leftIndex] < leftMax)
                {
                    ans += leftMax - height[leftIndex];
                }
                else
                {
                    leftMax = height[leftIndex];
                }
                leftIndex++;
            }
            else
            {
                if (height[rightIndex] < rightMax)
                {
                    ans += rightMax - height[rightIndex];
                }
                else
                {
                    rightMax = height[rightIndex];
                }
                rightIndex--;
            }
        }
        return ans;
    }
};
int main(int argc, char const *argv[])
{
    /* code */
    return 0;
}
